package fr.mywiki.business.entry;

import java.sql.SQLException;
import java.text.ParseException;
import java.util.Date;

import fr.mywiki.model.queries.DatabaseEntryController;

public class LockManager {
  /** Lock time in milliseconds : 30 minutes for the moment */
  public static final long lockTime = 30 * 60 * 1000;
  
  /**
   * Locks the entry given in parameter.
   * 
   * @param entry a <code>EntryLite</code> object.
   */
  public static void lock(EntryLite entry) throws SQLException {
    DatabaseEntryController.putLockOnEntry(entry);
  }
  
  /**
   * Perfoms the manual unlock of an entry.
   * 
   * @param entry a <code>EntryLite</code> object.
   */
  public static void unlock(EntryLite entry) throws SQLException {
    DatabaseEntryController.removeLockOnEntry(entry);
  }
  
  /**
   * Returns true iif the entry is currently locked, ie it has a lock whose time
   * is less than the lockTime.
   * 
   * @param entry a <code>EntryLite</code> object.
   * @return true or false.
   */
  public static boolean isLocked(EntryLite entry) throws SQLException, ParseException {
    Date lockDate = DatabaseEntryController.getEntryLock(entry);
    if (lockDate == null)
      return true;
    
    return (new Date().getTime() - lockDate.getTime()) < lockTime;
  }
}
